//
//  WD_ZiLiaoViewController.m
//  BUDING
//
//  Created by 景强 on 16/4/6.
//  Copyright © 2016年 HanWoonYong. All rights reserved.
//

#import "WD_ZiLiaoViewController.h"
#import "WD_ZiLiaoTableView.h"
#import "WD_ZiLiaoTwoTableView.h"
#import "WD_TimeLineModel.h"
#import "WD_OwnerModel.h"
#import "WD_RelatedModel.h"
#import "WD_EpModel.h"
#import "DrawView.h"
#import "WD_TableViewTimeLine.h"
#import "WD_CollectionViewCellTimeLine.h"
#import "WD_TableViewTwo.h"
#import "WD_ShowImageViewController.h"
@interface WD_ZiLiaoViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *arrayDataSource;
@property (nonatomic, strong) NSMutableArray *arrayIntro;
@property (nonatomic, strong) UIView *viewHead;
@property (nonatomic, strong) UIImageView *imageViewHead;
@property (nonatomic, strong) UILabel *labelNikcname;
@property (nonatomic, strong) UILabel *labelFollowing;
@property (nonatomic, strong) UILabel *labelFollower;
@property (nonatomic, strong) UIImageView *imageViewAvatar;
@property (nonatomic, strong) UISegmentedControl *seg;
@property (nonatomic, strong) UIView *viewSEG;
@property (nonatomic, strong) WD_ZiLiaoTableView *cell;
@property (nonatomic, strong) NSString *stringIntro;
@property (nonatomic, assign) NSInteger fuck;
@property (nonatomic, strong) WD_TimeLineModel *model;
@property (nonatomic, strong) NSMutableArray *arrayImage;
@property (nonatomic, strong) NSMutableArray *nameArr;
@property (nonatomic, strong) NSMutableArray *contentArr;
@property (nonatomic, strong) NSMutableArray *arrayUrl;
@property (nonatomic, strong) NSString *strUrl;
@property (nonatomic, strong) NSMutableArray *arrayEp;
@property (nonatomic, assign) NSInteger num;
@property (nonatomic, strong) WD_RelatedModel *modelRelat;



@end

@implementation WD_ZiLiaoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.fuck = 1;
    [self creatView];
    [self creatData];
    [self creatDataTwo];
    
    
}


- (void)creatView {
    
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.viewHead = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 300)];
    self.tableView.tableHeaderView = self.viewHead;
    self.imageViewHead = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 250)];
    
    [self.viewHead addSubview:self.imageViewHead];
    self.imageViewAvatar = [[UIImageView alloc] initWithFrame:CGRectMake(self.viewHead.frame.size.width / 2 - 50, self.viewHead.frame.size.height / 2 - 120, 90, 90)];
    
    self.imageViewAvatar.layer.masksToBounds = YES;
    self.imageViewAvatar.layer.cornerRadius = self.imageViewAvatar.frame.size.height / 2;
    self.imageViewAvatar.layer.borderWidth = 4.0f;
    self.imageViewAvatar.layer.borderColor = [[UIColor purpleColor] CGColor];
    
    [self.viewHead addSubview:self.imageViewAvatar];
    
    self.labelFollowing = [[UILabel alloc] initWithFrame:CGRectMake(self.viewHead.frame.size.width / 2 - 100, self.viewHead.frame.size.height / 2 + 20, 150, 50)];
    self.labelFollowing.textColor = [UIColor whiteColor];
    [self.viewHead addSubview:self.labelFollowing];
    
    
    self.labelFollower = [[UILabel alloc] initWithFrame:CGRectMake(self.viewHead.frame.size.width / 2 + 30, self.viewHead.frame.size.height / 2 + 20, 150, 50)];
    self.labelFollower.textColor = [UIColor whiteColor];
    [self.viewHead addSubview:self.labelFollower];
    
    
    self.labelNikcname = [[UILabel alloc] initWithFrame:CGRectMake(self.viewHead.frame.size.width / 2 - 95 , self.viewHead.frame.size.height / 2, 180, 30)];
    self.labelNikcname.layer.masksToBounds = YES;
    self.labelNikcname.layer.cornerRadius = 15;
    self.labelNikcname.backgroundColor = [UIColor colorWithRed:0.385 green:0.504 blue:1.000 alpha:1.000];
    self.labelNikcname.textColor = [UIColor whiteColor];
    // 文本的对齐方式
    self.labelNikcname.textAlignment = NSTextAlignmentCenter;
    [self.viewHead addSubview:self.labelNikcname];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    self.seg = [[UISegmentedControl alloc] initWithItems:@[@"资料", @"时间线"]];
    
    self.seg.frame = CGRectMake(0, 250, self.viewHead.frame.size.width, 30);
    
    self.seg.selectedSegmentIndex = 1;
    [self.seg addTarget:self action:@selector(segAction:) forControlEvents:UIControlEventValueChanged];
    
    self.seg.tintColor = [UIColor clearColor];
    NSDictionary *seletedTextAttribues = @{NSFontAttributeName:[UIFont systemFontOfSize:20]};
NSForegroundColorAttributeName:[UIColor whiteColor];
    [self.seg setTitleTextAttributes:seletedTextAttribues forState:UIControlStateSelected];
    NSDictionary *unselectedTextAttributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:15], NSForegroundColorAttributeName:[UIColor blackColor]};
    [self.seg setTitleTextAttributes:unselectedTextAttributes forState:UIControlStateNormal];
    [self.viewHead addSubview:self.seg];
    self.viewSEG = [[UIView alloc] initWithFrame:CGRectMake(230, self.seg.frame.size.height, 100, 3)];
    
    self.viewSEG.backgroundColor = [UIColor blackColor];
    [self.seg addSubview:self.viewSEG];
    [self.tableView registerClass:[WD_ZiLiaoTableView class] forCellReuseIdentifier:@"WD_ZiLiaoTableView"];
    [self.tableView registerClass:[WD_ZiLiaoTwoTableView class] forCellReuseIdentifier:@"WD_ZiLiaoTwoTableView"];
    [self.tableView registerClass:[WD_TableViewTimeLine class] forCellReuseIdentifier:@"WD_TableViewTimeLine"];
    [self.tableView registerClass:[WD_TableViewTwo class] forCellReuseIdentifier:@"WD_TableViewTwo"];
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"];
    [self.view addSubview:self.tableView];
    
    [self.view addSubview:self.tableView];
    
    
}

- (void)segAction:(UISegmentedControl *)seg {
    self.fuck = self.seg.selectedSegmentIndex;
    if (seg.selectedSegmentIndex == 0) {
        self.viewSEG.frame = CGRectMake(50, self.seg.frame.size.height, 100, 3);
        [self creatDataTwo];
        
    } else if (seg.selectedSegmentIndex == 1) {
        
        self.viewSEG.frame = CGRectMake(230, self.seg.frame.size.height, 100, 3);
        [self creatData];
        
        
    }
    
    
}





- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (self.fuck == 0) {
        
        if (indexPath.row == 0) {
            WD_ZiLiaoTableView *cell = [tableView dequeueReusableCellWithIdentifier:@"WD_ZiLiaoTableView"];
            //        self.cell.backgroundColor = [UIColor redColor];
            cell.labelIntro.text = self.stringIntro;
            //    self.cell.textLabel.text = @"ss";
            
            
            return cell;
            
        } else if (indexPath.row == 1) {
            
            
            WD_ZiLiaoTwoTableView *cell = [tableView dequeueReusableCellWithIdentifier:@"WD_ZiLiaoTwoTableView"];
            
            return cell;
            
        }
        
        
        
        
    } else if (self.fuck == 1) {
        
        self.model = self.arrayDataSource[indexPath.row];
        
        
        if ([self.model.type isEqual:@10001]) {
            
            WD_TableViewTwo *cell = [tableView dequeueReusableCellWithIdentifier:@"WD_TableViewTwo"];
            
            WD_OwnerModel *modelOwner = self.nameArr[indexPath.row];
            cell.model = modelOwner;
            
            WD_RelatedModel *modelRelat = self.contentArr[indexPath.row];
            cell.modelRelate = modelRelat;
            
            //        WD_EpModel *modelE = self.arrayEp[indexPath.row];
            WD_EpModel *ep = modelRelat.eps;
            cell.modelEp = ep;
            //        NSLog(@"%lu", (unsigned long)self.arrayEp.count);
            [cell.imageViewAvatar sd_setImageWithURL:[NSURL URLWithString:self.arrayUrl[indexPath.row]] placeholderImage:[UIImage imageNamed:@"占位图_"]];
            
            
            return cell;
            
            
            
            
        }
        if([self.model.type isEqual:@10002] || [self.model.type isEqual:@10010]) {
            
            
            WD_TableViewTimeLine *cell = [tableView dequeueReusableCellWithIdentifier:@"WD_TableViewTimeLine"];
            
            WD_OwnerModel *modelOwner = self.nameArr[indexPath.row];
            WD_RelatedModel *modelRelat = self.contentArr[indexPath.row];
            
            cell.model = modelOwner;
            cell.modelRelate = modelRelat;
            
            
#pragma block 方法调用
            
            // 弱引用
            __weak WD_ZiLiaoViewController *mySelf = self;
            cell.blockRow = ^(NSInteger num){
                
                mySelf.num = num;
                [mySelf giveNum];
            };
            
            
            WD_RelatedModel *model = self.contentArr[indexPath.row];
            NSMutableArray *mutable = [NSMutableArray arrayWithCapacity:0];
            for (NSDictionary *dic in model.images) {
                WD_imageModel *imageModel = [[WD_imageModel alloc] init];
                [imageModel setValuesForKeysWithDictionary:dic];
                [mutable addObject:imageModel];
            }
            
            //        cell.cellArray = self.arrayImage;
            cell.cellArray = [NSMutableArray arrayWithArray:mutable];
            [cell.imageViewAvatar sd_setImageWithURL:[NSURL URLWithString:self.arrayUrl[indexPath.row]] placeholderImage:[UIImage imageNamed:@"占位图_"]];
            
            return cell;
        }
        
        
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
        
        cell.backgroundColor = [UIColor redColor];
        return cell;
        
        
        
        
    }
    
    
    return nil;
    
}


- (void)giveNum {
    WD_ShowImageViewController *showVC = [[WD_ShowImageViewController alloc] init];
    showVC.numImage = self.num;
    showVC.related = self.modelRelat;
    [self.navigationController pushViewController:showVC animated:YES];
}

// block 方法
- (void)showAction:(NSMutableArray *)array {
    
    
    WD_ShowImageViewController *showVC = [[WD_ShowImageViewController alloc] init];
    
    
    
    [self.navigationController pushViewController:showVC animated:YES];
    NSLog(@">>>>  %ld <<<<", self.num);
    //    [self addChildViewController:showVC];
    //
    //    [self.view addSubview:showVC.view];
    //    showVC.view.frame = self.view.bounds;
    //    showVC.view.backgroundColor = [UIColor redColor];
    
}



















- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (self.fuck == 0) {
        return 2;
    } else if (self.fuck == 1) {
        return self.arrayDataSource.count;
    }
    
    
    return 0;
    
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.fuck == 0) {
        
        if (indexPath.row == 0) {
            return 100;
        } else if (indexPath.row == 1) {
            
            return 300;
            
            
            
        }
        
    } else if (self.fuck == 1) {
        
        if ([self.model.type isEqual:@10001]) {
            
            
            return 300;
        }
        
        if ([self.model.type isEqual:@10002] || [self.model.type isEqual:@10010]) {
            
            if (self.arrayUrl.count < 2) {
                return 200;
                
            } else if (self.arrayUrl.count > 4){
                
                return 500;
            }
            
            
        }
        
        
        
    }
    
    
    return 100;
    
}












- (void)creatData {
    
    self.arrayDataSource = [NSMutableArray arrayWithCapacity:0];
    self.strUrl = [[NSString alloc] init];
    self.arrayUrl = [NSMutableArray arrayWithCapacity:0];
    self.arrayEp = [NSMutableArray arrayWithCapacity:0];
    self.nameArr = [NSMutableArray arrayWithCapacity:0];
    self.contentArr = [NSMutableArray arrayWithCapacity:0];
    
    NSString *str = @"http://pudding.cc/api/v1/user/56ebdd08e697fadd718b4569/activity?apiKey=yuki_ios&auth1=fdda5733f59f234e3f474af3de9580e5&brand=Apple&channelId=App Store&deviceKey=5ECC523F-AED1-495A-8FD7-8CDA3770F7BD&limit=20&model=iPhone&os=iOS&osv=8.1.3&timestamp=1460027497&types=10001,10002,10007,10009,10010,10011&version=2.7.8";
    [JQAFNetTool GETNetWithUrl:str body:nil headerFile:nil response:JQJSON success:^(id result) {
        
        for (NSDictionary *dic in result) {
            
            WD_TimeLineModel *model = [[WD_TimeLineModel alloc] initWithDataSource:dic];
            [self.arrayDataSource addObject:model];
            [self.nameArr addObject:model.ownerObjects];
            if (model.relatedObjects) {
                [self.contentArr addObject:model.relatedObjects];
                
            } else {
                [self.arrayDataSource removeLastObject];
            }
            NSDictionary *dicOwnerObject = [dic objectForKey:@"ownerObject"];
            NSDictionary *dicAvatar = [dicOwnerObject objectForKey:@"avatar"];
            self.strUrl = [dicAvatar objectForKey:@"url"];
            [self.arrayUrl addObject:self.strUrl];
            
            
            
            
        }
        
        [self.tableView reloadData];
        
    } failure:^(NSError *error) {
        
    }];
    
    
    
    
}


- (void)creatDataTwo {
    
    self.arrayIntro = [NSMutableArray arrayWithCapacity:0];
    self.stringIntro = [[NSString alloc] init];
    NSString *str =
    @"http://pudding.cc/api/v1/user/553a7f5ce497fa09508b4e2c?apiKey=yuki_ios&auth1=0aed242e24a2582f185c79c7091db1b3&brand=Apple&channelId=App%20Store&deviceKey=5ECC523F-AED1-495A-8FD7-8CDA3770F7BD&model=iPhone&os=iOS&osv=8.1.3&timestamp=1460078967&version=2.7.8";
    //    http://pudding.cc/api/v1/user/5689eabce397fa4a088b458d?apiKey=yuki_ios&auth1=3666f0aa7d01fb031c00a498f7771dc2&brand=Apple&channelId=App%20Store&deviceKey=5ECC523F-AED1-495A-8FD7-8CDA3770F7BD&model=iPhone&os=iOS&osv=8.1.3&timestamp=1460195090&version=2.7.8
    [JQAFNetTool GETNetWithUrl:str body:nil headerFile:nil response:JQJSON success:^(id result) {
        // 背景图
        NSDictionary *dicBackGround = [result objectForKey:@"background"];
        NSString *strbackround = [dicBackGround objectForKey:@"url"];
        [self.imageViewHead sd_setImageWithURL:[NSURL URLWithString:strbackround] placeholderImage:[UIImage imageNamed:@"占位图_"]];
        // 头像
        NSDictionary *dicAvatar = [result objectForKey:@"avatar"];
        NSString *strAvatar = [dicAvatar objectForKey:@"url"];
        [self.imageViewAvatar sd_setImageWithURL:[NSURL URLWithString:strAvatar] placeholderImage:[UIImage imageNamed:@"占位图_"]];
        
        // 关注
        NSString *string = @"关注  ";
        NSString *strFollowing = [result objectForKey:@"followingCount"];
        NSString *string1 = [string stringByAppendingString:strFollowing];
        self.labelFollowing.text = string1;
        
        // 粉丝
        NSString *string2 = @"粉丝  ";
        NSString *strFollower = [result objectForKey:@"followerCount"];
        NSString *string3 = [string2 stringByAppendingString:strFollower];
        self.labelFollower.text = string3;
        
        // 昵称
        NSString *strNickName = [result objectForKey:@"nickname"];
        self.labelNikcname.text = strNickName;
        
        // 个性签名
        NSString *str = [result objectForKey:@"intro"];
        self.stringIntro = [NSString stringWithString:str];
        
        NSLog(@"%@", self.stringIntro);
        
        
        [self.tableView reloadData];
        
        
    } failure:^(NSError *error) {
        
    }];
    
    
    
    
    
    
}













- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
